"""
财务管理模型
"""
from sqlalchemy import Column, String, Integer, Numeric, Enum, ForeignKey, DateTime, func
from sqlalchemy.orm import relationship
from .base import Base


class PaymentRecord(Base):
    """支付记录表"""
    __tablename__ = "payment_records"
    
    order_id = Column(Integer, ForeignKey("orders.id"), nullable=False, comment="订单ID")
    payment_amount = Column(Numeric(10, 2), nullable=False, comment="支付金额")
    payment_method = Column(Enum("支付宝", "微信", "银行卡", "现金", "其他", name="payment_method"), 
                           nullable=False, comment="支付方式")
    payment_date = Column(DateTime, nullable=False, comment="支付时间")
    transaction_id = Column(String(100), comment="交易流水号")
    payment_status = Column(Enum("pending", "success", "failed", "refunded", name="payment_status"), 
                           default="pending", comment="支付状态")
    remark = Column(String(500), comment="备注")
    
    # 关系
    order = relationship("Order", back_populates="payment_records")
    
    def __repr__(self):
        return f"<PaymentRecord(id={self.id}, order_id={self.order_id}, payment_amount={self.payment_amount})>"


class RefundRecord(Base):
    """退款记录表"""
    __tablename__ = "refund_records"
    
    order_id = Column(Integer, ForeignKey("orders.id"), nullable=False, comment="订单ID")
    refund_amount = Column(Numeric(10, 2), nullable=False, comment="退款金额")
    refund_reason = Column(String(500), comment="退款原因")
    refund_date = Column(DateTime, nullable=False, comment="退款时间")
    refund_method = Column(Enum("原路退回", "银行转账", "现金", "其他", name="refund_method"), 
                          comment="退款方式")
    refund_status = Column(Enum("pending", "approved", "completed", "rejected", name="refund_status"), 
                          default="pending", comment="退款状态")
    approver_id = Column(Integer, ForeignKey("employees.id"), comment="审批人ID")
    remark = Column(String(500), comment="备注")
    
    # 关系
    order = relationship("Order", back_populates="refund_records")
    approver = relationship("Employee", back_populates="refund_approvals")
    
    def __repr__(self):
        return f"<RefundRecord(id={self.id}, order_id={self.order_id}, refund_amount={self.refund_amount})>"
